package com.hxzy.shop.controller;


import com.github.pagehelper.PageInfo;
import com.hxzy.shop.common.dto.auth.AuthMapStructConvert;
import com.hxzy.shop.common.dto.auth.req.*;
import com.hxzy.shop.common.dto.auth.resp.RoleAuthorityTreeResp;
import com.hxzy.shop.common.dto.auth.resp.RoleDetailResp;
import com.hxzy.shop.common.dto.auth.resp.RoleListResp;
import com.hxzy.shop.dto.auth.req.LoadUserReq;
import com.hxzy.shop.dto.auth.resp.LoadUserResp;
import com.hxzy.shop.entity.AdminRole;
import com.hxzy.shop.service.AuthService;
import com.hxzy.shop.service.impl.AuthServiceImpl;
import com.hxzy.shop.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController
@Api(tags = "后台用户管理")
@RequestMapping("/ums")
public class UmsController {
    @Resource(type = AuthServiceImpl.class)
    private AuthService umsService;

    @ApiOperation(value = "获取当前用户信息", notes = "获取当前用户信息")
    @PostMapping(value = "/loadUser")
    public Result<LoadUserResp> loadUser(@RequestBody LoadUserReq req) {
        return Result.success(umsService.loadUser(req.getAccountId()));
    }


    @ApiOperation(value = "总店角色分页")
    @GetMapping(value = "/role/roleList")
    public Result<PageInfo<RoleListResp>> roleList(@Valid RoleListReq req) {
      return Result.success( umsService.roleList(req));
    }

    @ApiOperation(value = "总让角色详情")
    @GetMapping(value = "/role/roleDetail/{roleId}")
    public Result<RoleDetailResp> roleDetail(@PathVariable(value = "roleId") Long roleId){
        AdminRole db = this.umsService.roleDetail(roleId);
        if(db==null){
           return  Result.failed("角色不存在");
        }

        //对象赋值  MapStruct来操作
        RoleDetailResp resp= AuthMapStructConvert.INSTANCE.toResp(db);
        return Result.success(resp);
    }

    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PutMapping(value = "/role/editRole")
    public Result<Object> editRole(@Validated @RequestBody EditRoleReq req) {
        int count=this.umsService.editRole(req);
        return count>0? Result.success():Result.failed();
    }


    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping(value = "/role/addRole")
    public Result<Object> addRole(@Validated @RequestBody AddRoleReq req){
        int count=this.umsService.addRole(req);
        return count>0? Result.success():Result.failed();
    }

    @ApiOperation(value = "更改角色状态", notes = "更改角色状态")
    @PostMapping(value = "/role/changeRoleState")
    public Result<Object> changeRoleState(@Valid @RequestBody ChangeRoleStateReq req){
        int count=this.umsService.changeRoleState(req);
        return count>0? Result.success():Result.failed();
    }

    @ApiOperation(value = "角色权限树", notes = "角色权限树")
    @GetMapping(value = "/role/roleAuthorityTree/{roleId}")
    public Result<List<RoleAuthorityTreeResp>> roleAuthorityTree(@PathVariable(value = "roleId") Long roleId) {
        return Result.success(umsService.roleAuthorityTree(roleId));
    }

    @ApiOperation(value = "修改角色权限", notes = "修改角色权限")
    @PostMapping(value = "/role/changeRoleAuthority")
    public Result<Object> changeRoleAuthority(@Validated @RequestBody ChangeRoleAuthorityReq req) {
        return Result.success(umsService.changeRoleAuthority(req));
    }

}
